package com.greenold.wechatpay.controller;

import cn.hutool.core.util.XmlUtil;
import com.greenold.common.core.domain.AjaxResult;
import com.greenold.order.service.IOrderService;
import com.greenold.order.domain.Order;
import com.greenold.wechatpay.service.IWxPayJsapiService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.Map;

/**
 * @author YueYang
 * Created on 2025/10/21 22:27
 * @version 1.0
 */
@Slf4j
@RestController
@RequestMapping("/wxpay/jsapi")
public class WxPayJsapiController {

    @Autowired
    private IWxPayJsapiService wxPayJsapiService;


    /**
     * 用户端微信小程序发起的jsapi支付请求
     *
     * @param order 前端发送过来的数据要参考order实体类或者order表
     * @return 响应给用户小程序前端支付所必须的参数
     */
    @RequestMapping("/createOrder/{userId}")
    public AjaxResult createOrder(@PathVariable Long userId, @RequestBody Order order) {

        log.info("用户端微信小程序发起的jsapi支付请求，参数为：{}", order);
        Map<String, String> map = wxPayJsapiService.jsapiPay(order, userId);
        return AjaxResult.success(map);
    }

    /**
     * 支付成功后微信官方发过来的回调通知
     */
    @PostMapping("/notify")
    public AjaxResult notify(HttpServletRequest request) {
        log.info("支付成功后微信官方发过来的回调通知，参数为：{}", request);
        return AjaxResult.success();
    }

}
